package heap

import (
	. "jvm/typedef"
)

// *******************************************************
func (self *Object) Bytes() []I1 {
	return self.data.([]I1)
}
func (self *Object) Shorts() []I2 {
	return self.data.([]I2)
}
func (self *Object) Chars() []U2 {
	return self.data.([]U2)
}
func (self *Object) Ints() []I4 {
	return self.data.([]I4)
}
func (self *Object) Longs() []I8 {
	return self.data.([]I8)
}
func (self *Object) Floats() []F4 {
	return self.data.([]F4)
}
func (self *Object) Doubles() []F8 {
	return self.data.([]F8)
}
func (self *Object) Refs() []*Object {
	return self.data.([]*Object)
}

// *******************************************************

func (self *Object) ArrayLength() U4 {
	length := 0
	switch self.data.(type) {
	case []I1:
		length = len(self.data.([]I1))
	case []I2:
		length = len(self.data.([]I2))
	case []U2:
		length = len(self.data.([]U2))
	case []I4:
		length = len(self.data.([]I4))
	case []I8:
		length = len(self.data.([]I8))
	case []F4:
		length = len(self.data.([]F4))
	case []F8:
		length = len(self.data.([]F8))
	case []*Object:
		length = len(self.data.([]*Object))
	default:
		panic("Not array!")
	}
	return U4(length)
}

// *******************************************************
// 数组拷贝
func ArrayCopy(src *Object, srcPos I4, dest *Object, destPos I4, length I4) {
	switch src.data.(type) {
	case []I1:
		_src := src.data.([]I1)[srcPos : srcPos+length]
		_dest := dest.data.([]I1)[destPos : destPos+length]
		copy(_dest, _src)
	case []I2:
		_src := src.data.([]I2)[srcPos : srcPos+length]
		_dest := dest.data.([]I2)[destPos : destPos+length]
		copy(_dest, _src)
	case []U2:
		_src := src.data.([]U2)[srcPos : srcPos+length]
		_dest := dest.data.([]U2)[destPos : destPos+length]
		copy(_dest, _src)
	case []I4:
		_src := src.data.([]I4)[srcPos : srcPos+length]
		_dest := dest.data.([]I4)[destPos : destPos+length]
		copy(_dest, _src)
	case []I8:
		_src := src.data.([]I8)[srcPos : srcPos+length]
		_dest := dest.data.([]I8)[destPos : destPos+length]
		copy(_dest, _src)
	case []F4:
		_src := src.data.([]F4)[srcPos : srcPos+length]
		_dest := dest.data.([]F4)[destPos : destPos+length]
		copy(_dest, _src)
	case []F8:
		_src := src.data.([]F8)[srcPos : srcPos+length]
		_dest := dest.data.([]F8)[destPos : destPos+length]
		copy(_dest, _src)
	case []*Object:
		_src := src.data.([]*Object)[srcPos : srcPos+length]
		_dest := dest.data.([]*Object)[destPos : destPos+length]
		copy(_dest, _src)
	default:
		panic("Not array!")
	}

}
